﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Dracham.MapEditor.Textures;
using Dracham.MapEditor.Zones;
using Dracham.MapEditor.Singleton;

namespace Dracham.MapEditor.Maps
{
    public class Zone
    {
        public Int32 Width { get; set; }
        public Int32 Height { get; set; }
        public String Name { get; set; }
        public String Id { get; set; }
        public String EnterZoneText { get; set; }

        public ZoneTile[,] Tiles { get; private set; }

        private Zone()
        {
            
        }

        public Zone(String name, String id, String enterZoneText, Int32 width, Int32 height)
        {
            this.Name = name;
            this.Id = id;
            this.EnterZoneText = enterZoneText;
            this.Width = width;
            this.Height = height;

            Tiles = new ZoneTile[width, height];

            for (Int32 x = 0; x < this.Width; x++)
            {
                for (Int32 y = 0; y < this.Height; y++)
                {
                    Tiles[x, y] = new ZoneTile();
                }
            }
        }

        public static Zone FromXmlNode(XmlNode mapNode)
        {
            Zone map = new Zone();

            map.Name = mapNode.Attributes["Name"].Value;
            frmLoader.SetStatusText("Loading zone: " + map.Name + ", Data");

            map.Width = Convert.ToInt32(mapNode.Attributes["Width"].Value);
            map.Height = Convert.ToInt32(mapNode.Attributes["Height"].Value);
            map.Id = mapNode.Attributes["Id"].Value;
            map.EnterZoneText = mapNode.Attributes["EnterZoneText"].Value;
            map.Tiles = new ZoneTile[map.Width, map.Height];

            Int32 x = 0;
            Int32 lx = 0;
            Int32 y = 0;
            frmLoader.SetStatusText("Loading zone: " + map.Name + ", Tile information");
            foreach (XmlNode node in mapNode.ChildNodes)
            {
                if (node.Name == "ZoneTile")
                {
                    x = Convert.ToInt32(node.Attributes["X"].Value);
                    y = Convert.ToInt32(node.Attributes["Y"].Value);

                    map.Tiles[x, y] = ZoneTile.FromXmlNode(node);
                }
            }

            return map;
        }

        public void WriteToXmlNode(ref XmlNode zoneNode)
        {
            //
            // remove all
            zoneNode.RemoveAll();

            //
            // append attributes
            zoneNode.Attributes.Append(DrachamMapEditor.ZoneManager.CreateAttribute("Name", this.Name));
            zoneNode.Attributes.Append(DrachamMapEditor.ZoneManager.CreateAttribute("Id", this.Id));
            zoneNode.Attributes.Append(DrachamMapEditor.ZoneManager.CreateAttribute("EnterZoneText", this.EnterZoneText));
            zoneNode.Attributes.Append(DrachamMapEditor.ZoneManager.CreateAttribute("Width", this.Width.ToString()));
            zoneNode.Attributes.Append(DrachamMapEditor.ZoneManager.CreateAttribute("Height", this.Height.ToString()));

            for (Int32 x = 0; x < this.Width; x++)
            {
                for (Int32 y = 0; y < this.Height; y++)
                {
                    zoneNode.AppendChild(this.Tiles[x, y].ToXmlNode(x, y));
                }
            }
        }

        public override string ToString()
        {
            return this.Name;
        }
    }
}
